//
//  ScanCodeController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/25.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "ScanCodeController.h"
#import "QRCodeReaderView.h"
#import "FJAudioTool.h"

@interface ScanCodeController ()<QRCodeReaderViewDelegate>
{
    QRCodeReaderView * readview;//二维码扫描对象
}
/**返回字段*/
@property (nonatomic,strong)NSString*tempStr;

@end

@implementation ScanCodeController

-(void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"扫一扫";
    self.view.backgroundColor = [UIColor whiteColor];
    [self initScan];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (readview) {
        [self reStartScan];
    }
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    if (readview) {
        [readview stop];
        readview.is_Anmotion = YES;
    }
}
-(void)backBtnClicked{
    [super backBtnClicked];
    BlockSafeRun(self.hanldBlock,@"");
}
#pragma mark 初始化扫描
- (void)initScan{
    if (readview) {
        [readview removeFromSuperview];
        readview = nil;
    }
    
    readview = [[QRCodeReaderView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    readview.is_AnmotionFinished = YES;
    readview.backgroundColor = [UIColor clearColor];
    readview.delegate = self;
//    readview.alpha = 0;
    
    [self.view addSubview:readview];
//    [UIView animateWithDuration:0.5 animations:^{
//        readview.alpha = 1;
//    }completion:^(BOOL finished) {
//
//    }];
    
}
#pragma mark -QRCodeReaderViewDelegate
- (void)readerScanResult:(NSString *)result
{
    readview.is_Anmotion = YES;
    [readview stop];
    DLog(@"=========二维码识别");
    DLog(@"%@",result);
    DLog(@"=========二维码识别");
    //播放扫描二维码的声音
    [FJAudioTool playSound:@"noticeMusic.wav"];
    
    [self accordingQcode:result];
    
    [self performSelector:@selector(reStartScan) withObject:nil afterDelay:1.5];
}
#pragma mark - 扫描结果处理
- (void)accordingQcode:(NSString *)str
{
    BlockSafeRun(self.hanldBlock,str);
    if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }

//    NSArray * array = [str componentsSeparatedByString:@"@"];
//    DLog(@"%@",array);
//    if (array.count<3) {
//        //导入钱包
//        NSString * codeStr = [FiveEncryption base64Dencode:str];
//        BlockSafeRun(self.hanldBlock,codeStr);
//        [self.navigationController popViewControllerAnimated:YES];
//
//        return;
//    }
//    if ([array.lastObject isEqualToString:@"asstes"]) {//资产收币码解析，跳转钱包转账
//        NSDictionary * dic = @{@"address":array[0],@"toValue":array[1]};
//        [SkipCenterTool runtimePush:@"AssetsTransferMoneyVC" dic:@{@"receiveDic":dic} nav:self.navigationController];
//    }else if ([array.lastObject isEqualToString:@"wallet"]){//钱包收币码解析，跳转钱包转账
//        NSDictionary * dic = @{@"address":array[0],@"toValue":array[1]};
//        [SkipCenterTool runtimePush:@"TransferMoneyController" dic:@{@"receiveDic":dic} nav:self.navigationController];
//    }
}
- (void)reStartScan
{
    readview.is_Anmotion = NO;
    if (readview.is_AnmotionFinished) {
        [readview loopDrawLine];
    }
    [readview start];
}
-(void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
    [FJAudioTool disposeSound:@"noticeMusic.wav"];
}

@end
